#include "Mydefine.h"

void LOCALPeriphInit()
{
	
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	USART_InitTypeDef UART_InitStructure;
	TIM_TimeBaseInitTypeDef TIMER1_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1, ENABLE);
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

	SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
	
	Sysms = SystemCoreClock / 8000;
	Sysus = Sysms / 1000;
	
	USART_DeInit(USART1);
	USART_DeInit(USART2);
	USART_DeInit(USART3);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);// USART1_Tx = PA9
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
	GPIO_Init(GPIOA, &GPIO_InitStructure);// USART2_Tx = PA2
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOB, &GPIO_InitStructure);// USART3_Tx = PB10
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure);// USART1_Rx = PA10
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
	GPIO_Init(GPIOA, &GPIO_InitStructure);// USART2_Rx = PA3
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
	GPIO_Init(GPIOB, &GPIO_InitStructure);// USART3_Rx = PB11
	
	UART_InitStructure.USART_BaudRate = 115200;
	UART_InitStructure.USART_Parity = USART_Parity_No;
	UART_InitStructure.USART_StopBits = USART_StopBits_1;
	UART_InitStructure.USART_WordLength = USART_WordLength_8b;
	UART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	UART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART1, &UART_InitStructure);
	USART_Init(USART2, &UART_InitStructure);
	USART_Init(USART3, &UART_InitStructure);
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART1, ENABLE);
	USART_Cmd(USART2, ENABLE);
	USART_Cmd(USART3, ENABLE);
	
	TIMER1_InitStructure.TIM_Period = 999;	// 72000000/(7199 + 1) * (999 + 1) = 100000US = 100MS
	TIMER1_InitStructure.TIM_Prescaler = 7199;	// TICK PERIOD IS 72000000/(7199 + 1)
	TIMER1_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIMER1_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM3, &TIMER1_InitStructure);
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
	
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_Init(&NVIC_InitStructure);	
	
	TIM_Cmd(TIM3, ENABLE);	
}

void Usart1Send(const char *Str, const uint16_t length)
{
	uint16_t i = length;
	USART_ClearFlag(USART1, USART_FLAG_TC);
	for(; i>0; --i){
		USART_SendData(USART1, *Str++);
		while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));
  }
}

void Usart2Send(const char *Str, const uint16_t length)
{
	uint16_t i = length;
	USART_ClearFlag(USART2, USART_FLAG_TC);
	for(; i>0; --i){
		USART_SendData(USART2, *Str++);
		while(RESET == USART_GetFlagStatus(USART2, USART_FLAG_TC));
  }
}

void Usart3Send(const char *Str, const uint16_t length)
{
	uint16_t i = length;
	USART_ClearFlag(USART3, USART_FLAG_TC);
	for(; i>0; --i){
		USART_SendData(USART3, *Str++);
		while(RESET == USART_GetFlagStatus(USART3, USART_FLAG_TC));
  }
}

void Delayus(unsigned long us)
{
	unsigned long temp;
	SysTick->LOAD = us * Sysus;
	SysTick->VAL = 0x00;
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
	do{
		temp = SysTick->CTRL;
	}while( ( temp & 0x01 ) && !( temp & (1<<16) ));
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
	SysTick->VAL = 0X00;
}

void Delayms(unsigned long ms)
{
	u32 temp;
	SysTick->LOAD = (unsigned long)ms * Sysms;
	SysTick->VAL = 0x00;
	SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
	do{
		temp=SysTick->CTRL;
	}while( ( temp & 0x01 ) && !( temp & (1<<16) ));
	SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
	SysTick->VAL = 0X00;
}
